import { Request, toast } from '../../utils/util.js'
let request = new Request()
let app = getApp()
var numberInter = null
Page({
  data: {
    userType : 1,
    query: {
      status: 0
    },
    orderList: [],
    showCancelConfirm: false,
    currentIndex: -1,

    number1 : 0,
    number2: 0
  },

  selectToggle(e) {
    let status = parseInt(e.currentTarget.dataset.status)
    if (this.data.query.status == status) {
      return
    }
    this.setData({ orderList: [] })
    this.setData({ 'query.status': status })
    this.selectComponent('#pagination').initLoad()
  },

  load(e) {
    let rows = e.detail.list
    let page = e.detail.page
    let orderList = Object.assign([], this.data.orderList)
    rows.forEach(item => {
      item.delivery = { remarks: item.remarks, consignee: item.consignee, mobile: item.mobile, province: item.province, city: item.city, district: item.district, address: item.address }
      orderList.push(item)
    })

    this.setData({ orderList: orderList })
  },

  updateOrderList (index) {
    let orderList = this.data.orderList
    orderList.splice(index, 1)
    this.setData({ orderList: orderList })
    if (orderList.length == 0) {
      this.selectComponent('#pagination').initLoad()
    }
  },

  cancelOrder(e) {
    let index = e.currentTarget.dataset.index
    this.setData({ currentIndex: index })
    this.setData({ showCancelConfirm: true })
  },

  cancelCallback(e) {
    this.setData({ showCancelConfirm: false })
    if (e.detail == 0) {
      let index = this.data.currentIndex
      let orderId = this.data.orderList[index].order_id
      request.post('order/cancel', res => {
        if (res.success) {
          toast('取消订单成功')
          this.updateOrderList(index)
        } else {
          toast(res.msg)
        }
      }, { orderId: orderId }).showLoading()
    }
  },


  confirmComplete (e) { //确定收货
    let index = e.currentTarget.dataset.index
    wx.showModal({
      title: '确定收货',
      content: '请确定已经收到商品',
      success : res => {
        if (res.confirm) {
          this.completeOrder(index)
        }
      }
    })
  },

  completeOrder (index) {
    let orderId = this.data.orderList[index].order_id
    request.post('order/complete', res => {
      if (res.success) {
        toast('确定成功')
        this.updateOrderList(index)
      } else {
        toast(res.msg)
      }
    }, { id: orderId })
  },

  getNumber () {
    var req = new Request()
    req.get('order/number', res => {
      if(res.success) {
        var data = res.data
        if('number1' in data) {
          this.setData({number1 : data.number1})
        }
        if ('number2' in data) {
          this.setData({ number2: data.number2 })
        }
      }
    })
  },

  onLoad () {
    let userInfo = app.globalData.userInfo
    if (userInfo.user_type == 2) {
      this.setData({ userType: userInfo.user_type })
    } else {
      
    }

    this.getNumber()
    numberInter = setInterval(() => {
      this.getNumber()
    }, 3000)

  },

  onUnload () {
    clearInterval(numberInter)
  }

})
